package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService travelCommendService;





    /*
    根据type获取推荐游记
    动作：GET
    资源：/travels/commends
    参数：type=xx
     */
    @GetMapping("/commends")
    public Object getTravelCommends(TravelQueryObject qo) {
        //按照推荐时间排序
        qo.setOrderBy("schedule desc");
        return travelCommendService.selectForType(qo);
    }



    /*
    根据id获取游记
    动作：GET
    资源：/travels/{id}
    参数：
     */
    /*@GetMapping("/{id}")
    public Object getTravelContent(@PathVariable Long id) {

        return travelService.get(id);
    }*/


    @GetMapping
    public Object getTravelsPage(TravelQueryObject qo) {
        //设置为发布并且公开的
        qo.setState(Travel.STATE_RELEASE);
        //根据创建时间排序
        qo.setOrderBy("t.createTime desc");
        return travelService.selectForList(qo);
    }

    @PutMapping("/{id}")
    public Object update(Travel travel) {
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

    @GetMapping("/{id}")
    public Object getTravels(@PathVariable Long id) {

        return travelService.get(id);
    }

    @PostMapping
    public Object save(Travel travel) {
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

}
